﻿Imports System.Data
Imports System.Data.OleDb

Public Class ConnectDB

    Private stconnect As String = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=QLNhaTro"
    Private conn As OleDbConnection
    Private cmd As OleDbCommand

    Public Function Connect() As Boolean
        conn = New OleDbConnection(stconnect)
        Try
            conn.Open()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Không thể kết nối dữ liệu " + ex.Message)
        End Try
        Return True
    End Function

    Public Sub Close()
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Dispose()
    End Sub

    Public Function Execute(ByVal sSql As String)
        cmd = New OleDbCommand()
        Connect()
        cmd.Connection() = conn
        cmd.CommandText = sSql
        Return cmd.ExecuteNonQuery()
    End Function

    Public Function GetData(ByVal sSql As String) As DataTable
        Dim dt As New DataTable
        Connect()
        cmd = New OleDbCommand(sSql, conn)
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(dt)
        Close()
        Return dt
    End Function


End Class
